package com.xiaotu.storage.converter;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

/**
 * @类名 AConverter
 * @日期 2017年2月21日
 * @作者 高海军
 * @功能 数据转换器基类
 */
public abstract class AConverter
{

    public static final String STRING_CONVERT = "String";

    public static final String NUMBER_CONVERT = "NumberFormate";
    
    public static final String NUMBERARRAY_CONVERT = "NumberArray";

    public static final String RATE_CONVERT = "Rate";

    public static final String RATEARRAY_CONVERT = "RateArray";

    public static final String INTEGER_CONVERT = "IntegerFormate";

    public static final String DATE_CONVERT = "Date";

    public static final String STRINGARRAY_CONVERT = "StringArray";

    public static final String GET_NUMBER_CONVERT = "GetNumberFormate";


    public static final String TIMESTAMP_CONVERT = "Timestamp";

    /**
     * 数据转换
     *
     * @param value     要转换的值
     * @param target    整个数据对象
     * @param needThrow 当不满足转换条件时，是否跑出异常
     * @return 转换结果
     */
    public Object convert(String value, JSONObject target, boolean needThrow)
    {
        if (StringUtils.isEmpty(value))
            return null;
        return doConvert(value, target, needThrow);
    }

    protected abstract Object doConvert(String value, JSONObject target,
            boolean needThrow);
}
